# ---------------------------------------------------------------------------------------------------- #
# finalcode.R
# ---------------------------------------------------------------------------------------------------- #
#
# Date: 2014-06-18
# Authors: Jaime E. Settle, Robert M. Bond, Lorenzo Coviello, Christopher J. Fariss, James H. Fowler, Jason J. Jones, Adam D. I. Kramer, Cameron Marlow
#
# All inquires about the models and code should be sent to Jaime Settle and Chris Fariss
# Contact: jsettle@wm.edu cjf0006@gmail.com
#
# Title:From Posting to Voting: The Effects of Political Competition on Online Political Engagement
# Most recent version available at: http://ssrn.com/abstract=2232099
#
# Journal: Political Science Research & Methods
#
# Copyright (c) 2014, The R code is under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
# For more information see: http://creativecommons.org/licenses/by-nc-sa/3.0/us/
# All rights reserved.
#
# The data are available by request though Facebook Inc. Interested parties need to contact Facebook Inc. directly to obtain secure access to the data.
#
# R code begins below
# ---------------------------------------------------------------------------------------------------- #
#
#
#
#
#------------FILES---------------#
#There are two root files from Facebook that were used in these analyses:

#umsample.rda is the binary R file which contains the unmatched sample that was pulled directly from Facebook's on site secure servers
#jsettle_matchedsample_votingdata is a text file that was pulled that includes information on whether the users in the matched sample clicked "I voted"

#-----------Matching--------------------#

#--------------Matching for the Status Update Analysis----------------#
#This data moves from the unmatched sample that was pulled from Facebook's servers to a matched sample. These are the files that can be used to recreate Tables 4, 5 and 6 in the Supporting Information.  

# to install the libries used here run the following code: install.packages(c("foreign", "MatchIt", "gtools"))
library(foreign)
library(MatchIt)
library(gtools)

# load umsample.rda
load(file="/home/jfowler/Discussion/JaimeAnalysisFiles/umsample.rda") # note that this file path and those below all are in reference to folders on a facebook server on site at facebook headquarters.


# here we time the creation of the matched data usining the matchit() function from the MatchIt library
Sys.time()
umsample2 <- umsample[complete.cases(umsample),]
sample3 <- matchit(battleground ~ age + male + statustwen + friendtwen + college, data=umsample2, method="exact")
sample <- match.data(sample3)
Sys.time()

# save data objects
sampledecile <- sample
save(sampledecile, file="/home/jfowler/Discussion/JaimeAnalysisFiles/sampledecile.rda")
sampledecilesum <- sample3
save(sampledecilesum, file="/home/jfowler/Discussion/JaimeAnalysisFiles/sampledecilesum.rda")


#----------Adding new variables--------------#
#To move from the raw matched sample (sampledecile) to the file that includes summary variables (sample2), see sample2.R

#-----------------Matching for the Status Update Analysis--------------------#
library(foreign)
library(MatchIt)
library(gtools)

#jsettle_matchedsample_votingdata is the file that was pulled from Facebook's servers that includes the information about whether the user had clicked the "I voted button." It was identical to sample2 from above but included the new variable, and because I had already renamed all the variables in sample2, I just used that to name the new data file. 

# load jsettle_matchedsample_votingdata which is a text file
vote <- read.delim("jsettle_matchedsample_votingdata", header=FALSE, na.strings="NULL")
load(file="/home/jfowler/Discussion/JMData/sample2.rda")

# extract names and add one new name
names(vote)[2:97] <- names(sample2)
names(vote)[98] <- c("voted")

# keep only the vote object from the sample2 object
vote <- vote[,2:98]

# remove the sample2 object from memory
remove(sample2)

vote$voted[which(vote$voted=="NULL")] <- NA

#To move from vote to vote2 (which is referenced in the data below), see vote2.R. This adds summary statistics for the whole campaign period. Thus, for the voting analsyis, vote2 becomes the new subject-level file.

#-----------Figure 1: Voting Bar Plot------------------------#

N1 <- length(which(vote2$battleground==0))  # number of blackout users
P1 <- length(which(vote2$voted2==1 & vote2$battleground==0))  # number of blackoutvote2rs
N1.1 <- length(which(vote2$voted2 < 99 & vote2$battleground==0))  # number of blackoutvote2rs

# create proporations and upper and lower bounds
SAMPLEPROP1 <- P1/N1
SQRT1 <- sqrt((SAMPLEPROP1*(1-SAMPLEPROP1))/N1)
SEU1 <- SAMPLEPROP1 + SQRT1
SEL1 <- SAMPLEPROP1 - SQRT1

# create proporations and upper and lower bounds
SAMPLEPROP1.1 <- P1/N1.1
SQRT1.1 <- sqrt((SAMPLEPROP1.1*(1-SAMPLEPROP1.1))/N1.1)
SEU1.1 <- SAMPLEPROP1.1 + SQRT1.1
SEL1.1 <- SAMPLEPROP1.1 - SQRT1.1

# create combined object for testing
blackout <- rbind(P1, N1, SAMPLEPROP1, SQRT1, SEU1, SEL1, N1.1, SAMPLEPROP1.1, SQRT1.1, SEU1.1, SEL1.1)

N2 <- length(which(vote2$battleground==1))  # number of blackout users
P2 <- length(which(vote2$voted2==1 & vote2$battleground==1))  # number of blackoutvote2rs
N2.1 <- length(which(vote2$voted2<99 & vote2$battleground==1))  # number of blackoutvote2rs

# create proporations and upper and lower bounds
SAMPLEPROP2 <- P2/N2
SQRT2 <- sqrt((SAMPLEPROP2*(1-SAMPLEPROP2))/N2)
SEU2 <- SAMPLEPROP2 + SQRT2
SEL2 <- SAMPLEPROP2 - SQRT2

# create proporations and upper and lower bounds
SAMPLEPROP2.1 <- P2/N2.1
SQRT2.1 <- sqrt((SAMPLEPROP2.1*(1-SAMPLEPROP2.1))/N2.1)
SEU2.1 <- SAMPLEPROP2.1 + SQRT2.1
SEL2.1 <- SAMPLEPROP2.1 - SQRT2.1

# create combined object for testing
battle <- rbind(P2, N2, SAMPLEPROP2, SQRT2, SEU2, SEL2, N2.1, SAMPLEPROP2.1, SQRT2.1, SEU2.1, SEL2.1)

votediff <- matrix(NA, 3, 6)
i <- 1

votediff [1,1] <- SAMPLEPROP1
votediff [2,1] <- SEU1
votediff [3,1] <- SEL1

votediff [1,2] <- SAMPLEPROP2
votediff [2,2] <- SEU2
votediff [3,2] <- SEL2


# create combined object for testing
test <- prop.test(c(battle[1,i], blackout[1,i]), c(battle[2,i], blackout[2,i]), conf.level=.68)

votediff [1,3] <- test$estimate[1]-test$estimate[2]
votediff [2,3] <- test$conf.int[2]
votediff [3,3] <- test$conf.int[1]


votediff [1,4] <- SAMPLEPROP1.1
votediff [2,4] <- SEU1.1
votediff [3,4] <- SEL1.1

votediff [1,5] <- SAMPLEPROP2.1
votediff [2,5] <- SEU2.1
votediff [3,5] <- SEL2.1

test <- prop.test(c(battle[1,i], blackout[1,i]), c(battle[7,i], blackout[7,i]), conf.level=.68)

votediff [1,6] <- test$estimate[1]-test$estimate[2]
votediff [2,6] <- test$conf.int[2]
votediff [3,6] <- test$conf.int[1]

# save analysis objects
save(votediff, file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/votediff.rda")

# Moving locally to plot
votediff <- votediff*100

# setup plot parameters
COLOR <- c("grey", "red")
COLOR.NAME <- c("grey", "red")
par(mar=c(5,6,5,.5), cex=1, font=2, font.lab=2)

# plot call
barplot(votediff[1,1:2], beside=T,col=COLOR,font=2,font.lab=2,ylim=c(0,50), ylab="", yaxt="n")

# objects for plotting
h <- votediff[2,1:2]
l <- votediff[3,1:2]
x <- c(1:2)
ind <- c(.7,1.9)

# loop through the objects to make the plot
for(i in 1:2){
    lines(c(ind[i],ind[i]), c(l[i], h[i]), lwd=1, lty=1, col=1)
    text(ind[i], 12, labels = paste(round(votediff[1, x[i]], 3), "%", sep=""), cex = 1.5)
}

text(1.3, 45, paste(round(votediff[1,3], 2), "%", sep="") , col="royalblue4", cex=2)
text(1.3, 42, paste("(", round(votediff[3,3], 2), "-", round(votediff[2,3], 2), ")", "%", sep=""), col="royalblue4", cex=1.5)

axis(side=2,at=seq(from=0, to=50, by=10), label=c( "0%","10%", "20%", "30%", "40%", "50%"), las=2, cex=.5)
axis(side=1, at=c(.7,1.9), label=c("Blackout", "Battleground"), las=1, cex=.1, line = 1, lwd=0)
axis(side=2, at=25, label=c("Percent Clicking"), las=0, cex=.7, line=3, font=2, tick=F)
mtext(c("Difference in Percent of Users Clicking 'I Voted' \n Battleground vs. Blackout"), las=0, cex=1.1, line=1, font=2, side=3, at=1.3)

abline(h=0, lty=3, col=COLOR[1])

box() # add box around plot region



#-----------------Matching for the Voting  Analysis--------------------#
#This next part of the analysis goes along with the text on pages 15-16: "Therefore, we conducted an additional matching process to isolate the effect of political status update posting on voting. In each process, we match users who post a political status update between August 25th and November 3rd to people who do not post a political status update, but who look just like them on age, gender, education, battleground status, the number of status updates they post before August 25th, the number of friends they have, and whether they post a non-political status update in the summer. This helps to isolate the effect of engagement on voting, controlling for the treatment status of the respondent.

# load libraries
library(foreign)
library(MatchIt)
library(gtools)

# load data
load(file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/vote2.rda")

# select predermined columns from vote2 data object
v2 <- vote2[,c(12, 1, 5,13,18, 19, 8, 160, 173, 174, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 155,157, 158,160, 161, 163, 164, 166, 167, 169, 170, 172, 22, 23, 27, 29, 30, 32, 33, 37, 39, 40, 42, 43, 47, 49, 50, 52, 53, 57, 59, 60, 62, 63, 67, 69, 70, 72, 73, 77,80, 82, 83, 87, 89, 90, 7, 175:177)]

# remove vote2 data object from memory
remove(vote2)

# Match users on whether they posted a non-emotional political post in the campaign season on age, male, statustwen, friendtwen, college, summeremononpolpostsdic and their battleground status.

Sys.time()
v <- v2[complete.cases(v2),]

# create the matched data usining the matchit() function from the MatchIt library
vote3 <- matchit(campaignnonemopolpostsdic ~ age + male + statustwen + friendtwen + college + summeremononpolpostsdic + battleground, data=v, method="exact")
votematch_nonemopol<- match.data(vote3)
Sys.time()

# save objects
save(votematch_nonemopol, file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/votematch_nonemopol.rda")
votematch_nonemopolsum <- vote3
save(votematch_nonemopolsum, file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/votematch_nonemopolsum.rda")


#------------------------Figure 2-----------------------------#

#Making the Plot to Show Effects of Battleground on Non-Emotional Political Speech---------------
# This is created from the vote3 file, the individual level file. It creates a table that shows the effect of battleground status on status update posting in five stages of the study period. The numbers input into the "vec" object are the numbers of the columns in the vote file that correspond to political posts that are political but not emotional. 

vec <- c(33, 43, 155, 73, 83) # create object to find only specified columns when creating objects for testing below
campaignnonemobattleeffects <- as.list(rep(NA,length(vec)))

# create more ojects for testing
for (i in 1:length(vec)){

    P1 <- length(which(vote3[,vec[i]]>0 & vote3$battleground==1))
    N1 <- length(which(vote3$battleground==1))

    SAMPLEPROP1 <- P1/N1
    SQRT1 <- sqrt((SAMPLEPROP1*(1-SAMPLEPROP1))/N1)
    SEU1 <- SAMPLEPROP1 + SQRT1
    SEL1 <- SAMPLEPROP1 - SQRT1

    P2 <- length(which(vote3[,vec[i]]>0 & vote3$battleground==0))
    N2 <- length(which(vote3$battleground==0))

    SAMPLEPROP2 <- P2/N2
    SQRT2 <- sqrt((SAMPLEPROP2*(1-SAMPLEPROP2))/N2)
    SEU2 <- SAMPLEPROP2 + SQRT2
    SEL2 <- SAMPLEPROP2 - SQRT2

    test <- prop.test(c(P1, P2), c(N1, N2), conf.level=.68)

    SAMPLEPROP3 <- test$estimate[1]-test$estimate[2]
    SEU3 <- test$conf.int[2]
    SEL3 <- test$conf.int[1]

    campaignnonemobattleeffects[[i]] <- as.data.frame(cbind(c(N1, P1, SAMPLEPROP1, SEU1, SEL1), c(N2, P2, SAMPLEPROP2, SEU2, SEL2), c(SAMPLEPROP1,SAMPLEPROP2, SAMPLEPROP3[[1]], SEU3[[1]], SEL3[[1]])))

}

save(campaignnonemobattleeffects, file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/campaignnonemobattleeffects.rda")


#---------Making the figure-------------#

# create a matrix
campaignnonemobattle <- matrix(NA, 5,5)

# fill the matrix from a vector that holds the objects for testing
for(i in 1:length(campaignnonemobattleeffects)){
    campaignnonemobattle[,i] <- campaignnonemobattleeffects[[i]]$V3
}

campaignnonemobattle <- campaignnonemobattle*100

# set parameter values for plotting
COLOR <- c("grey", "red")
COLOR.NAME <- c("grey", "red")
par(mar=c(5,8,1,.5), cex=1, font=2, font.lab=2)

# plot call
barplot(campaignnonemobattle[3,], beside=T,col=COLOR[2],font=2,font.lab=2,ylim=c(-1,1), ylab="", yaxt="n")

# create more objects for plotting
h <- campaignnonemobattle[4,]
l <- campaignnonemobattle[5,]
x <- seq(1:ncol(campaignnonemobattle))
ind <- c(.7,1.9,3.1,4.3, 5.5)

# loop through plotting objects
for(i in 1:ncol(campaignnonemobattle)){
    lines(c(ind[i],ind[i]), c(l[i], h[i]), lwd=1, lty=1, col=1)
}
      
axis(side=2,at=seq(from=-1, to=1, by=.5), label=c("-1.0%", "-0.5%", "0%", "0.5%", "1.0%"), las=2, cex=.5)
axis(side=1, at=c(.7,1.9,3.1,4.3, 5.5), label=c("January 1- \n June 8", "June 9- \n August 24", "August 25- \n Nov. 3", "Nov. 4", "Nov. 5- \n January 31"), las=1, cex=.1, line = 1, lwd=0)
axis(side=2, at=0, label=c("Difference between Percentage of\nBattleground and Blackout Users who Post\na Non-Emotional Political Status Message"), las=0, cex=.7, line=3, font=2, tick=F)

# these plotting objects are optional. we didn't put them into the final version
#mtext(c("Difference in Percent of Political Users \n Battleground vs. Blackout"), las=0, cex=1.1, line=1, font=2, side=3, at=3)
#mtext(c("Difference in Percent of Political Users \n Battleground vs. Blackout \n of All Users"), las=0, cex=1.1, line=1, font=2, side=3, at=3)

abline(h=0, lty=3, col=COLOR[1])

#lines(c(-1000,1000),c(0,0),lty="dotted") # add 0-line  
box() # add box around plot region




#-------------------Table 1-------------------------#

#The table is made from the campaignnonemobattle table created above. 



#--------------------Figure 3-------------------------#

#This code moves from the matched data file to the summary analysis
# # # # # # # # # # # # # # # # ##

# load more data
load(file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/votematch_nonemopol.rda")

# create testing objects
P1 <- length(which(votematch_nonemopol$voted==1& votematch_nonemopol$campaignnonemopolpostsdic==1))
N1 <- length(which(votematch_nonemopol$campaignnonemopolpostsdic==1))
N1.1 <- length(which(votematch_nonemopol$voted<99 & votematch_nonemopol$campaignnonemopolpostsdic==1))
SAMPLEPROP1 <- P1/N1
SQRT1 <- sqrt((SAMPLEPROP1*(1-SAMPLEPROP1))/N1)
SEU1 <- SAMPLEPROP1 + SQRT1
SEL1 <- SAMPLEPROP1 - SQRT1

SAMPLEPROP1.1 <- P1/N1.1
SQRT1.1 <- sqrt((SAMPLEPROP1.1*(1-SAMPLEPROP1.1))/N1.1)
SEU1.1 <- SAMPLEPROP1.1 + SQRT1.1
SEL1.1 <- SAMPLEPROP1.1 - SQRT1.1

P2 <- length(which(votematch_nonemopol$voted==1& votematch_nonemopol$campaignnonemopolpostsdic==0))
N2 <- length(which(votematch_nonemopol$campaignnonemopolpostsdic==0))
N2.1 <- length(which(votematch_nonemopol$voted<99 & votematch_nonemopol$campaignnonemopolpostsdic==0))

SAMPLEPROP2 <- P2/N2
SQRT2 <- sqrt((SAMPLEPROP2*(1-SAMPLEPROP2))/N2)
SEU2 <- SAMPLEPROP2 + SQRT2
SEL2 <- SAMPLEPROP2 - SQRT2

SAMPLEPROP2.1 <- P2/N2.1
SQRT2.1 <- sqrt((SAMPLEPROP2.1*(1-SAMPLEPROP2.1))/N2.1)
SEU2.1 <- SAMPLEPROP2.1 + SQRT2.1
SEL2.1 <- SAMPLEPROP2.1 - SQRT2.1

P3 <- length(which(votematch_nonemopol$voted==1& votematch_nonemopol$campaignnonemopolpostsdic==1 & votematch_nonemopol$battleground==1))
N3 <- length(which(votematch_nonemopol$campaignnonemopolpostsdic==1 & votematch_nonemopol$battleground==1))
N3.1 <- length(which(votematch_nonemopol$voted<99 & votematch_nonemopol$campaignnonemopolpostsdic==1 & votematch_nonemopol$battleground==1))

SAMPLEPROP3 <- P3/N3
SQRT3 <- sqrt((SAMPLEPROP3*(1-SAMPLEPROP3))/N3)
SEU3 <- SAMPLEPROP3 + SQRT3
SEL3 <- SAMPLEPROP3 - SQRT3

SAMPLEPROP3.1 <- P3/N3.1
SQRT3.1 <- sqrt((SAMPLEPROP3.1*(1-SAMPLEPROP3.1))/N3.1)
SEU3.1 <- SAMPLEPROP3.1 + SQRT3.1
SEL3.1 <- SAMPLEPROP3.1 - SQRT3.1


P4 <- length(which(votematch_nonemopol$voted==1& votematch_nonemopol$campaignnonemopolpostsdic==0 & votematch_nonemopol$battleground==1))
N4 <- length(which(votematch_nonemopol$campaignnonemopolpostsdic==0 & votematch_nonemopol$battleground==1))
N4.1 <- length(which(votematch_nonemopol$voted<99 & votematch_nonemopol$campaignnonemopolpostsdic==0 & votematch_nonemopol$battleground==1))

SAMPLEPROP4 <- P4/N4
SQRT4 <- sqrt((SAMPLEPROP4*(1-SAMPLEPROP4))/N4)
SEU4 <- SAMPLEPROP4 + SQRT4
SEL4 <- SAMPLEPROP4 - SQRT4

SAMPLEPROP4.1 <- P4/N4.1
SQRT4.1 <- sqrt((SAMPLEPROP4.1*(1-SAMPLEPROP4.1))/N4.1)
SEU4.1 <- SAMPLEPROP4.1 + SQRT4.1
SEL4.1 <- SAMPLEPROP4.1 - SQRT4.1

P5 <- length(which(votematch_nonemopol$voted==1& votematch_nonemopol$campaignnonemopolpostsdic==1 & votematch_nonemopol$battleground==0))
N5 <- length(which(votematch_nonemopol$campaignnonemopolpostsdic==1 & votematch_nonemopol$battleground==0))
N5.1 <- length(which(votematch_nonemopol$voted<99 & votematch_nonemopol$campaignnonemopolpostsdic==1 & votematch_nonemopol$battleground==0))

SAMPLEPROP5 <- P5/N5
SQRT5 <- sqrt((SAMPLEPROP5*(1-SAMPLEPROP5))/N5)
SEU5 <- SAMPLEPROP5 + SQRT5
SEL5 <- SAMPLEPROP5 - SQRT5

SAMPLEPROP5.1 <- P5/N5.1
SQRT5.1 <- sqrt((SAMPLEPROP5.1*(1-SAMPLEPROP5.1))/N5.1)
SEU5.1 <- SAMPLEPROP5.1 + SQRT5.1
SEL5.1 <- SAMPLEPROP5.1 - SQRT5.1

P6 <- length(which(votematch_nonemopol$voted==1& votematch_nonemopol$campaignnonemopolpostsdic==0 & votematch_nonemopol$battleground==0))
N6 <- length(which(votematch_nonemopol$campaignnonemopolpostsdic==0 & votematch_nonemopol$battleground==0))
N6.1 <- length(which(votematch_nonemopol$voted<99 & votematch_nonemopol$campaignnonemopolpostsdic==0 & votematch_nonemopol$battleground==0))

SAMPLEPROP6 <- P6/N6
SQRT6 <- sqrt((SAMPLEPROP6*(1-SAMPLEPROP6))/N6)
SEU6 <- SAMPLEPROP6 + SQRT6
SEL6 <- SAMPLEPROP6 - SQRT6

SAMPLEPROP6.1 <- P6/N6.1
SQRT6.1 <- sqrt((SAMPLEPROP6.1*(1-SAMPLEPROP6.1))/N6.1)
SEU6.1 <- SAMPLEPROP6.1 + SQRT6.1
SEL6.1 <- SAMPLEPROP6.1 - SQRT6.1

# mke a data.frame to creat tables
votematch_nonemopol.emoeffect <- as.data.frame(cbind(c(N1, P1, SAMPLEPROP1, SEU1, SEL1), c(N1.1, P1, SAMPLEPROP1.1, SEU1.1, SEL1.1),c(N2, P2, SAMPLEPROP2, SEU2, SEL2), c(N2.1, P2, SAMPLEPROP2.1, SEU2.1, SEL2.1),c(N3, P3, SAMPLEPROP3, SEU3, SEL3),c(N3.1, P3, SAMPLEPROP3.1, SEU3.1, SEL3.1),  c(N4, P4, SAMPLEPROP4, SEU4, SEL4),  c(N4.1, P4, SAMPLEPROP4.1, SEU4.1, SEL4.1), c(N5, P5, SAMPLEPROP5, SEU5, SEL5),  c(N5.1, P5, SAMPLEPROP5.1, SEU5.1, SEL5.1), c(N6, P6, SAMPLEPROP6, SEU6, SEL6),  c(N6.1, P6, SAMPLEPROP6.1, SEU6.1, SEL6.1)))

# add names to the new data.frame
names(votematch_nonemopol.emoeffect) <- c("emo", "emo_login", "nonemo", "nonemo_login","emoinbattle", "emoinbattle_login", "nonemoinbattle", "nonemoinbattle_login", "emoinblackout","emoinblackout_login", "nonemoinblackout", "nonemoinblackout_login")

# save the new data.frame for later
save(votematch_nonemopol.emoeffect, file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/votematch_nonemopol.emoeffect.rda")


#------------Balance Test------------#

# create index objects for the loop below
ind <- c(1, 3:7, 9, 58, 59, 63, 64)
ind2 <- c(58, 59, 63, 64, 39)

# create a matrix to collect new data
matchedmeans <- as.data.frame(matrix(NA, length(ind) + length(ind2), 3))

# loop through testing objects to created weighted means, which are stored in the matrix object created above
for (i in 1:length(ind)){
    matchedmeans[i,1] <- weighted.mean(votematch_nonemopol[,ind[i]][votematch_nonemopol$campaignnonemopolpostsdic==1], votematch_nonemopol$weights[votematch_nonemopol$campaignnonemopolpostsdic==1])
    matchedmeans[i,2] <- weighted.mean(votematch_nonemopol[,ind[i]][votematch_nonemopol$campaignnonemopolpostsdic==0], votematch_nonemopol$weights[votematch_nonemopol$campaignnonemopolpostsdic==0])
    matchedmeans[i,3] <- weighted.mean(votematch_nonemopol[,ind[i]][votematch_nonemopol$campaignnonemopolpostsdic==1], votematch_nonemopol$weights[votematch_nonemopol$campaignnonemopolpostsdic==1]) - weighted.mean(votematch_nonemopol[,ind[i]][votematch_nonemopol$campaignnonemopolpostsdic==0], votematch_nonemopol$weights[votematch_nonemopol$campaignnonemopolpostsdic==0])
}

for (i in 1:length(ind2)){

    matchedmeans[length(ind) + i,1]<- length(which(votematch_nonemopol[,ind2[i]]>0 & votematch_nonemopol$campaignnonemopolpostsdic==1))/ length(which(votematch_nonemopol$campaignnonemopolpostsdic==1))
    matchedmeans[length(ind) + i,2]<- length(which(votematch_nonemopol[,ind2[i]]>0 & votematch_nonemopol$campaignnonemopolpostsdic==0))/ length(which(votematch_nonemopol$campaignnonemopolpostsdic==0))
    matchedmeans[length(ind) + i,3]<- length(which(votematch_nonemopol[,ind2[i]]>0 & votematch_nonemopol$campaignnonemopolpostsdic==1))/ length(which(votematch_nonemopol$campaignnonemopolpostsdic==1)) - length(which(votematch_nonemopol$statuscount>0 & votematch_nonemopol$campaignnonemopolpostsdic==0))/ length(which(votematch_nonemopol$campaignnonemopolpostsdic==0))

}

# add names to the rows for tables
rownames(matchedmeans)[1:length(ind)] <- names(votematch_nonemopol)[ind]
rownames(matchedmeans)[(length(ind) + 1): (length(ind) + length(ind2))] <- c("Primary Pol Prop", "Primary Emo Pol Prop", "Summer Pol Prop", "Summer Emo Pol Prop", "At Least One Campaign Post Prop")

# add column names for tables
names(matchedmeans) <- c("Emotional Political", "Not Emotional Political", "Difference")

# rename and save object
matchedmeans.votematch_nonemopol <- matchedmeans
save(matchedmeans.votematch_nonemopol, file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/matchedmeans.votematch_nonemopol.rda")


#This code makes the plot to show the effects of Non Emotional Political Speech on Voting ---------------#

test1 <- prop.test(c(votematch_nonemopol.emoeffect[2,1],votematch_nonemopol.emoeffect[2,3]), c(votematch_nonemopol.emoeffect[1,1],votematch_nonemopol.emoeffect[1,3]), conf.level=.68)

t1 <- test1$estimate[1]-test1$estimate[2]
t1h <- test1$conf.int[2]
t1l <- test1$conf.int[1]

tdiff <- rbind(cbind(t1,t1h,t1l))
tdiff <- tdiff*100


# create objects and set parameters for plotting
COLOR <- c("firebrick1", "gray")
COLOR.NAME <- c("grey", "red")
par(mar=c(5,6,5,.5), cex=1, font=2, font.lab=2)

barplot(t(votematch_nonemopol.emoeffect[3,c(1,3)]),beside=T,col=COLOR,font=2,font.lab=2,ylim=c(0,1), ylab="", yaxt="n", xlab="", xaxt="n")

h <- t(votematch_nonemopol.emoeffect[4,c(1,3)])
l <- t(votematch_nonemopol.emoeffect[5,c(1,3)])
ind <- seq(from=1.5, to = 1.5*3, by=1)
x <- c(1,3)

for(i in 1:4){
    lines(c(ind[i],ind[i]), c(l[i], h[i]), lwd=1, lty=1, col=1)
    text(ind[i], .09, labels = paste(round(t(votematch_nonemopol.emoeffect[3,x[i]])*100,2), "%", sep=""), cex = 1.5)
}

text(2, .75, paste(round(tdiff[1,1], 2), "%", sep="") , col="royalblue4", cex=1.5)
text(2, .7, paste("(", round(tdiff[1,3], 2), " - ", round(tdiff[1,2], 2), ")", "%", sep=""), col="royalblue4")


axis(side=2,at=seq(from=0, to=1, by=.1), label=c( "0%","10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"), las=2, cex=.05, line=0)
axis(side=1, at=ind[1:2], label=c("Political", "NonPolitical"), las=1, cex=.1, line = 1, lwd=0)
axis(side=2, at=.5, label=c("Percent Clicking I Voted"), las=0, cex=.7, line=3, font=2, tick=F)
#axis(side=1, at=1.3, label=names(emovoteprop)[j], las=0, cex=.3, line=0, font=2, tick=F)
mtext(c("Percent of Matched Users Clicking 'I Voted' \n Political vs. Non-Political"), las=0, cex=1.1, line=1, font=2, side=3, at=2)

abline(h=0, lty=3, col=COLOR[1])



# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #
#---------------SI----------------#
# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #

#Figure 1 is simply schematic code. There is no empirical analysis

#Tables 1-3 were generated using publically available data. This information was used to support the authors' determination of whether a state was a "battleground" or "blackout" state.

#Tables 4-6 are discussed above when the matching process on the original data was described. 

#--------------------Table 7-----------------------#

#-----------------------Table of Various Proportions----------------------------#

P1 <- length(which(vote3$campaignpol75posts > 0)) # political posters in campaign season
  P2 <- length(which(vote3$campaignemopol75posts > 0)) # emotional political posters in campaign season

  N1 <- dim(vote3)[1] # all users
  N2 <- length(which(vote3$totalposts > 0))#all active users in total study period
  N3 <- length(which(vote3$campaigntotal > 0)) # all active users in campaign eperiod
  N4 <- length(which(vote3$campaignpol75posts > 0)) # all political posters in campaign season


SAMPLEPROP1 <- P1/N1
SQRT1 <- sqrt((SAMPLEPROP1*(1-SAMPLEPROP1))/N1)
SEU1 <- SAMPLEPROP1 + SQRT1
SEL1 <- SAMPLEPROP1 - SQRT1

SAMPLEPROP2 <- P1/N2
SQRT2 <- sqrt((SAMPLEPROP2*(1-SAMPLEPROP2))/N2)
SEU2 <- SAMPLEPROP2 + SQRT2
SEL2 <- SAMPLEPROP2 - SQRT2

SAMPLEPROP3 <- P1/N3
SQRT3 <- sqrt((SAMPLEPROP3*(1-SAMPLEPROP3))/N3)
SEU3 <- SAMPLEPROP3 + SQRT3
SEL3 <- SAMPLEPROP3 - SQRT3

SAMPLEPROP4 <- P2/N1
SQRT4 <- sqrt((SAMPLEPROP4*(1-SAMPLEPROP4))/N1)
SEU4 <- SAMPLEPROP4 + SQRT4
SEL4 <- SAMPLEPROP4 - SQRT4

SAMPLEPROP5 <- P2/N2
SQRT5 <- sqrt((SAMPLEPROP5*(1-SAMPLEPROP5))/N2)
SEU5 <- SAMPLEPROP5 + SQRT5
SEL5 <- SAMPLEPROP5 - SQRT5

SAMPLEPROP6 <- P2/N3
SQRT6 <- sqrt((SAMPLEPROP6*(1-SAMPLEPROP6))/N3)
SEU6 <- SAMPLEPROP6 + SQRT6
SEL6 <- SAMPLEPROP6 - SQRT6

SAMPLEPROP7 <- P2/N4
SQRT7 <- sqrt((SAMPLEPROP7*(1-SAMPLEPROP7))/N4)
SEU7 <- SAMPLEPROP7 + SQRT7
SEL7 <- SAMPLEPROP7 - SQRT7


proptable <- rbind(c(P1, N1, SAMPLEPROP1, SEL1, SEU1), c(P1, N2, SAMPLEPROP2, SEL2, SEU2), c(P1, N3, SAMPLEPROP3, SEL3, SEU3), c(P2, N1, SAMPLEPROP4, SEL4, SEU4), c(P2, N2, SAMPLEPROP5, SEL5, SEU5),  c(P2, N3, SAMPLEPROP6, SEL6, SEU6),c(P2, N4, SAMPLEPROP7, SEL7, SEU7))

save(proptable, file="/home/jfowler/Discussion/JaimeAnalysisFiles/Voting/proptable.rda")

proptable[,3:5] <- proptable[,3:5]*100
xtable(proptable, digits=3)


#--------------------Table 8-----------------------#
#This can be derived from the vote3 file



#--------------------End of File-----------------------#

